import csv

# initialize
BOQ = False # Beginning of Question
EOQ = False # End of Question
question = []
ndx = 0

# ~~ pick one to digest ~~ notice that every 1 to 2 years there is a new question pool from a Class
#with open("Technician Pool and Syllabus 2022-2026 Public Release March 7 2022.txt","r") as test, open("questionDB.txt","w") as output:
#with open("General Class Pool and Syllabus 2023-2027 Public Release April 15 2023.txt","r") as test, open("questionDB.txt","w") as output:
with open("Amateur Extra Class Pool and Syllabus 2020-2024 Public Release March 5 2020.txt","r") as test, open("questionDB.txt","w") as output:
    database = csv.writer(output, delimiter="\t")
    for line in test:
        line = line.replace("\n","")        # remove newlines at the end of the line
        if len(line) > 5 and EOQ:         # not an unusually short line and the end-of-question is found 
            BOQ = True                      # must be a new question
            EOQ = False                     # reset end of question
            question.append(line)           # add first item in list
        elif len(line) > 5 and BOQ:       # not an unusually sort line and the question started
            question.append(line)           # add in new answer to list
#            print(line)                    # debug statement
        if line.find("~~") >= 0:           # ~~ is end-of-question marker, -1 if not found
            print(question)                 # debug statement
            database.writerow(question)     # write list (current question) with \t delimiters
            BOQ = False                     # reset BOQ
            EOQ = True                      # completed question
            question = []                   # reset list
print(question)                           # debug

# now use excel to import these files, add columns and do some splitting